package com.example.tellwin.view;

import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tellwin.R;
import com.example.tellwin.base.BaseDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : LinZePeng
 * @date : 2020/11/13
 * @Describe ：
 **/
public class SelectListDialog extends BaseDialog {


    List<CharSequence> stringList = new ArrayList<>();

    private OnListSelectListener onListSelectListener;
    private RecyclerView listRv;
    private TextView cancelTv;
    private TextView showTv;

    private ListAdapter listAdapter;

    public void setOnListSelectListener(OnListSelectListener onListSelectListener) {
        this.onListSelectListener = onListSelectListener;
    }

    public SelectListDialog(@NonNull Context context, List<CharSequence> stringList) {
        super(context, R.style.ActionSheetDialogStyle);
        this.stringList = stringList;
    }

    public void setStringList(List<CharSequence> stringList) {
        this.stringList = stringList;
        if(listAdapter!=null){
            listAdapter.setStringList(stringList);
        }

    }

    public void show(TextView tv) {
        super.show();
        this.showTv = tv;
    }

    public SelectListDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_select_list);
        Window window = this.getWindow();
        //设置位置在屏幕底部
        window.setGravity(Gravity.BOTTOM);
        //设置弹入弹出动画
        window.setWindowAnimations(R.style.BottomDialog_Animation);
        //设置为全屏dialog
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;

        window.setAttributes(params);
        initView();
        initEvent();
    }

    private void initView() {
        listRv = (RecyclerView) findViewById(R.id.list_rv);
        cancelTv = (TextView) findViewById(R.id.cancel_tv);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        listRv.setLayoutManager(linearLayoutManager);
        listRv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                int position = parent.getChildAdapterPosition(view);
                if (position != 0) {
                    outRect.top = getContext().getResources().getDimensionPixelOffset(R.dimen.dp_0_5);
                }
            }
        });
        listAdapter =new ListAdapter();
        listRv.setAdapter(listAdapter);
        listAdapter.setStringList(stringList);
    }

    private void initEvent() {
        cancelTv.setOnClickListener(v -> {
            dismiss();
        });

    }

    public interface OnListSelectListener {
        void onListSelect(int position, String str);
    }

    public interface OnAddressSelectListener {
        void onAddressSelect(String province, String city, String area);
    }

    class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListViewHolder>{

        Context context;
        List<CharSequence>stringList =new ArrayList<>();

        public void setStringList(List<CharSequence> stringList) {
            this.stringList = stringList;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public ListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            context = parent.getContext();
            return new ListViewHolder(LayoutInflater.from(context).inflate(R.layout.item_list ,parent,false));
        }

        @Override
        public void onBindViewHolder(@NonNull ListViewHolder holder, int position) {
            holder.itemTv.setText(stringList.get(position).toString());
            holder.itemTv.setOnClickListener(v -> {
                if(onListSelectListener !=null){
                    onListSelectListener.onListSelect(position , stringList.get(position).toString());
                }
                if(showTv!=null){
                    showTv.setText(stringList.get(position).toString());
                }
                dismiss();
            });
        }

        @Override
        public int getItemCount() {
            return stringList.size();
        }

        class ListViewHolder extends RecyclerView.ViewHolder{
            TextView itemTv;
            public ListViewHolder(@NonNull View itemView) {
                super(itemView);
                itemTv = itemView.findViewById(R.id.item_tv);
            }
        }
    }

}
